package org.hyjiacan.deadqq.server.dao;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.text.MessageFormat;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

import org.hyjiacan.deadqq.common.entity.User;
import org.hyjiacan.deadqq.server.db.DBUtil;

public class UserDAO implements IUserDAO {

	@Override
	public boolean add(User user) {
		PreparedStatement ps = DBUtil.getPreparedStatement(DBUtil
				.getString("user add"));
		try {
			ps.setLong(1, user.getNumber());
			ps.setString(2, user.getNickname());
			ps.setString(3, user.getPassword());
			ps.setInt(4, user.getAge());
			ps.setBoolean(5, user.isMale());
			ps.setDate(6, user.getBirthday());
			ps.setString(7, user.getAvatar());
			ps.setString(8, user.getSignature());
			ps.setInt(9, user.getLevel());
			ps.setBoolean(10, user.isOnline());
			ps.setDate(11, user.getRegdate());
			ps.setString(12, user.getName());
			System.out.println("执行SQL:" + ps.toString());
			ps.execute();
			return true;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return false;
		}
	}

	@Override
	public boolean delete(User user) {
		try {
			Statement stmt = DBUtil.getStatement();
			stmt.execute(java.text.MessageFormat.format(
					DBUtil.getString("user delete"), user.getNumber()));
			return true;
		} catch (SQLException ex) {
			Logger.getLogger(UserDAO.class.getName()).log(Level.SEVERE, null,
					ex);
		}
		return false;
	}

	@Override
	public boolean edit(User user) {
		try {
			String sql = DBUtil.getString("user update");
			PreparedStatement ps = DBUtil.getPreparedStatement(sql);
			ps.setString(1, user.getNickname());
			ps.setString(2, user.getPassword());
			ps.setInt(3, user.getAge());
			ps.setBoolean(4, user.isMale());
			ps.setDate(5, user.getBirthday());
			ps.setString(6, user.getAvatar());
			ps.setString(7, user.getSignature());
			ps.setInt(8, user.getLevel());
			ps.setBoolean(9, user.isOnline());
			ps.setString(10, user.getName());
			ps.setLong(11, user.getNumber());
			System.out.println("执行SQL:" + ps.toString());
			return ps.execute();
		} catch (SQLException ex) {
			Logger.getLogger(UserDAO.class.getName()).log(Level.SEVERE, null,
					ex);
		}
		return false;
	}

	@Override
	public User queryById(int id) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public User query(long number) {
		Statement stmt = DBUtil.getStatement();
		try {
			String sql = MessageFormat.format(
					DBUtil.getString("user query by number"),
					String.valueOf(number));
			ResultSet rs = stmt.executeQuery(sql);

			if (rs.next()) {
				return DBUtil.getUser(rs);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}

	@Override
	public List<User> query(String name) {
		// TODO Auto-generated method stub
		return null;
	}
}
